package socket.server;

import model.Game;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;

/**
 * Created by admin
 * <p>
 * Date :2020/10/15
 * <p>
 * Description :
 * <p>
 * Version :1.0
 */
public class ConnectHandler extends Thread {
    private static final Logger logger = LoggerFactory.getLogger(ConnectHandler.class);

    private int clientId;

    private Socket socket;

    private Game game;

    public ConnectHandler(Socket socket, Game game) {
        this.socket = socket;
        this.game = game;
    }

    @Override
    public void run() {
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String msg = bufferedReader.readLine();;
            while (!"exit".equals(msg)){
                if(Strings.isNotBlank(msg)){
                    logger.info("recived client msg : {}", msg);
                    bufferedWriter.write("hello, " + msg);
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                }
                msg = bufferedReader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
